import { BrowserRouter, HashRouter, Navigate, Route, Routes } from "react-router-dom"
import { RouterItem } from "../unit/type"
import router from "./router"
import { Suspense } from "react"



const routerView = () => {
  const { mode, routerList } = router
  const readerHome = (arr: RouterItem[]) => {
    return arr && arr.map((item, index) => {
      return <Route key={index} path={item.path}
        element={item.to ? <Navigate to={item.to}></Navigate> : <item.element></item.element>}></Route>
    })
  }
  return (
    <Suspense fallback={<div>路由加载中</div>}>
      {
        mode === 'history' ? <BrowserRouter>
        <Routes>
          {
            readerHome(router.routerList)
          }
        </Routes>
        </BrowserRouter> : <HashRouter>
          <Routes>
            {
              readerHome(router.routerList)
            }
          </Routes>
        </HashRouter>
      }
    </Suspense>
  )
}
export default routerView